<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"
            v-show="this.$store.state.user.info.deptId == 100">
            <el-form-item label="企业名称" prop="deptName" v-show="this.$store.state.user.info.deptId == 100">
                <el-input v-model="queryParams.deptName" placeholder="请输入企业名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="状态" prop="status" v-show="this.$store.state.user.info.deptId == 100">
                <el-select v-model="queryParams.status" placeholder="部门状态" clearable>
                    <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item v-show="this.$store.state.user.info.deptId == 100">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-if="refreshTable" v-loading="loading" :data="deptList" row-key="deptId"
            :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            :row-style="{ height: '30px' }" border :cell-style="{ padding: '0px' }" height="400px">
            <el-table-column label="序号" type="index" align="center" prop="index" :index="indexMethod"></el-table-column>
            <el-table-column prop="deptName" label="企业名称" width="250" align="center" :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    <!-- 使用按钮作为可点击元素，并绑定点击事件 -->
                    <el-button size="small" v-if="scope.row.type == '1'"
                        @click="handleLook(scope.row, scope.$index + 1)" type="text">
                        {{ scope.row.deptName }}
                    </el-button>
                    <el-button size="small" v-else type="text" style="color: black;"> {{ scope.row.deptName
                        }}</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="address" label="企业地址" width="300" align="center"
                :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="companyOrigin" label="企业来源" :show-overflow-tooltip="true" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.enterprise_source" :value="scope.row.companyOrigin" />
                </template>
            </el-table-column>
            <!-- <el-table-column prop="abbreviate" label="企业简称" show-overflow-tooltip="true"></el-table-column> -->
            <el-table-column prop="nature" label="企业性质" :show-overflow-tooltip="true" align="center">

                <template slot-scope="scope">
                    <dict-tag :options="dict.type.enterprise_nature" :value="scope.row.nature" />
                </template>
            </el-table-column>

            <el-table-column prop="contacts" label="联系人" align="center" width="90"
                :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="phone" label="联系电话" width="100" :show-overflow-tooltip="true"
                align="center"></el-table-column>
            <el-table-column prop="legalPerson" label="法人代表" align="center"
                :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="companyType" label="企业类别" align="center">

                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_org_type" :value="scope.row.companyType" />
                </template>
            </el-table-column>
            <!-- <el-table-column prop="type" label="类别" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_dept_type" :value="scope.row.type" />
        </template>
      </el-table-column> -->
            <el-table-column label="企业logo" align="center" prop="url">

                <template slot-scope="scope">
                    <el-button v-if="scope.row.url != null" @click.stop="handleView(scope.row.url)" size="small"
                        type="text">附件</el-button>
                </template>
            </el-table-column>
            <el-table-column prop="foundTime" label="成立时间" :show-overflow-tooltip="true" width="90" align="center">

                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.foundTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="80" align="center">

                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        <!-- 添加或修改部门对话框 用于平台管理员 -->
        <el-dialog :title="title" :visible.sync="openAdmin" width="700" append-to-body>
            <div v-if="opt != 'add'">
                <el-descriptions title="">
                    <el-descriptions-item label="序号">
                        <el-tag size="medium">{{ index }}</el-tag>
                    </el-descriptions-item>
                </el-descriptions>
            </div>
            <el-form ref="form" :model="form" :rules="rulesAdmin" label-width="80px">
                <el-row>
                    <!-- <el-col :span="12" style="height: 55px;">
            <el-form-item label="上级部门" prop="parentId">
              <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择上级部门" />
            </el-form-item>
          </el-col> -->
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="显示排序" prop="orderNum">
                            <el-input-number style="width: 280px" v-model="form.orderNum" controls-position="right"
                                :min="0" :disabled="isShow" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="企业名称" prop="deptName">
                            <el-input v-model="form.deptName" placeholder="请输入企业名称" :disabled="isShow" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="企业来源" prop="companyOrigin">
                            <el-select v-model="form.companyOrigin" style="width: 280px" placeholder="请选择企业来源"
                                :disabled="isShow">
                                <el-option v-for="dict in dict.type.enterprise_source" :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="企业性质" prop="nature">
                            <el-select style="width: 280px" v-model="form.nature" placeholder="请选择企业性质"
                                :disabled="isShow">
                                <el-option v-for="dict in dict.type.enterprise_nature" :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="所属行业" prop="industry">
                            <el-select v-model="form.industry" placeholder="请选择所属行业" style="width: 280px"
                                :disabled="isShow">
                                <el-option v-for="dict in dict.type.customer_industry" :key="dict.value"
                                    :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                </el-row>


                <el-row>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="法人代表" prop="legalPerson">
                            <el-input v-model="form.legalPerson" placeholder="请输入法人代表" maxlength="20"
                                :disabled="isShow" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="成立时间" prop="foundTime">
                            <el-date-picker v-model="form.foundTime" type="date" value-format="yyyy-MM-dd"
                                :picker-options="pickerStartOptions" placeholder="选择日期" style="width: 280px"
                                :disabled="isShow">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="联系人" prop="contacts">
                            <el-input v-model="form.contacts" placeholder="请输入联系人" maxlength="20" :disabled="isShow" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="联系电话" prop="phone">
                            <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="11" :disabled="isShow" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="注册资金" prop="registeredFund">
                            <el-input v-model.number="form.registeredFund" placeholder="请输注册资金" :disabled="isShow" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="企业类别">
                            <el-radio-group v-model="form.companyType" :disabled="isShow">
                                <el-radio v-for="dict in dict.type.sys_org_type" :key="dict.value"
                                    :label="dict.value">{{
            dict.label
        }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="12" style="height: 55px;">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
            </el-form-item>
          </el-col> -->
                </el-row>

                <el-row>
                    <el-form-item label="企业地址" prop="address">
                        <el-input v-model="form.address" type="textarea" placeholder="请输入企业地址" :disabled="isShow" />
                    </el-form-item>
                </el-row>

                <el-row>
                    <el-form-item label="经营范围" prop="businessScope">
                        <el-input v-model="form.businessScope" type="textarea" placeholder="请输入经营范围"
                            :disabled="isShow" />
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-row>
                        <el-form-item label="企业介绍" prop="companyProfile">
                            <el-input v-model="form.companyProfile" type="textarea" placeholder="请输入企业介绍"
                                :disabled="isShow" />
                        </el-form-item>
                    </el-row>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="企业状态">
                            <el-radio-group v-model="form.status" :disabled="isShow">
                                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                                    :label="dict.value">{{
            dict.label
        }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- <el-row>
          <el-form-item label="企业logo">
            <file-view :type="fileType" :businessId="businessId" :businessCode="businessCode" @change="getFile"
              :key="refreshKey"></file-view>
          </el-form-item>
        </el-row> -->

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm" v-if="!isShow">保 存</el-button>
                <el-button @click="cancelAdmin">关 闭</el-button>
            </div>
        </el-dialog>
        <!-- 添加或修改部门对话框 用于机构管理员 -->
        <el-dialog :title="title" :visible.sync="open" width="700" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>

                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="显示排序" prop="orderNum">
                            <el-input-number style="width: 280px" v-model="form.orderNum" controls-position="right"
                                :min="0" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="部门状态">
                            <el-radio-group v-model="form.status">
                                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                                    :label="dict.value">{{
            dict.label
        }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="上级部门" prop="parentId">
                            <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer"
                                placeholder="选择上级部门" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" style="height: 55px;">
                        <el-form-item label="部门名称" prop="deptName">
                            <el-input v-model="form.deptName" placeholder="请输入部门名称" />
                        </el-form-item>
                    </el-col>

                </el-row>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">保 存</el-button>
                <el-button @click="cancel">关 闭</el-button>
            </div>
        </el-dialog>
        <!-- 图片预览 -->
        <el-dialog :visible.sync="dialogVisible" append-to-body>
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        <!-- pdf预览 -->
        <el-dialog :visible.sync="dialogVisiblePdf" append-to-body>
            <pdf v-for="item in pageTotal" :src="pdfUrl" :key="item" :page="item"></pdf>
        </el-dialog>
        <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
            <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                    <div class="el-upload__tip" slot="tip">
                        <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
                    </div>
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                        @click="importTemplate">下载模板</el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">保 存</el-button>
                <el-button @click="upload.open = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listDept, getDept, delDept, addDept, updateDept, listDeptExcludeChild } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import FileView from "@/components/FileView/index"
import { getToken } from "@/utils/auth";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import pdf from 'vue-pdf'

export default {
    name: "Dept",
    dicts: ['enterprise_nature', 'sys_normal_disable', 'customer_industry', 'sys_dept_type', 'sys_org_type', 'enterprise_source'],
    components: { Treeselect, FileView },

    data() {
        return {
            pickerStartOptions: {
                disabledDate: (time) => {
                    return time.getTime() > Date.now() - 8.64e7;
                }
            },
            opt: "edit",
            isShow: false,
            index: null,
            deptType: null,
            userNewId: "",
            openAdmin: false,
            fileType: 0,
            // 总条数
            total: 0,
            businessId: null,
            businessCode: "enterprise_logo",
            refreshKey: 0,
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 表格树数据
            deptList: [],
            // 部门树选项
            deptOptions: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部展开
            isExpandAll: true,
            // 重新渲染表格状态
            refreshTable: true,
            // 企业导入参数
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 是否更新已经存在的用户数据
                updateSupport: 0,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/system/dept/importData"
            },
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 15,
                deptName: undefined,
                companyType: "0",
                status: undefined
            },
            disabled: false,
            dept: null,
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                parentId: [
                    { required: true, message: "上级部门不能为空", trigger: "blur" }
                ],
                deptName: [
                    { required: true, message: "部门名称不能为空", trigger: "blur" }
                ],
                orderNum: [
                    { required: true, message: "显示排序不能为空", trigger: "blur" }
                ],
            },
            rulesAdmin: {
                companyOrigin: [
                    { required: true, message: "企业来源不能为空", trigger: "change" }
                ],
                // industry: [
                //   { required: true, message: "所属行业不能为空", trigger: "change" }
                // ],
                // parentId: [
                //   { required: true, message: "上级部门不能为空", trigger: "blur" }
                // ],
                deptName: [
                    { required: true, message: "企业名称不能为空", trigger: "blur" }
                ],
                orderNum: [
                    { required: true, message: "显示排序不能为空", trigger: "blur" }
                ],
                // nature: [
                //   { required: true, message: "企业性质不能为空", trigger: "blur" }
                // ],
                // contacts: [
                //   { required: true, message: "联系人不能为空", trigger: "blur" }
                // ],
                // address: [
                //   { required: true, message: "企业地址不能为空", trigger: "blur" }
                // ],
                // phone: [
                //   { required: true, message: "联系电话不能为空", trigger: "blur" },
                // ],

            },
            //pdf预览
            dialogImageUrl: '',
            dialogImageUrlPdf: '',
            pageTotal: null,
            dialogVisible: false,
            dialogVisiblePdf: false,
        };
    },
    created() {
        if (this.$store.state.user.info.deptId == 100) {
            this.showSearch = true
        } else {
            this.showSearch = false
        }
        //alert(this.$store.state.user.info.depeId)
        //his.getId();
        this.getList();
    },
    watch: {
        'form.parentId'(newval, oldval) {
            for (let index = 0; index < this.dept.length; index++) {
                const element = this.dept[index];
                if (element.deptId == newval && element.type == 1) {
                    this.disabled = false
                } if (element.deptId == newval && element.type == 0) {
                    this.disabled = true
                }
            }
        }
    },
    methods: {
        handleLook(row, index) {
            this.reset();
            this.fileType = 1;
            this.opt = "edit";
            this.isShow = true;
            this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
            this.refreshKey++;
            if (row.fileId == "" || row.fileId == null || row.fileId == undefined) {
                this.businessId = row.deptId;
            } else {
                this.businessId = row.fileId;
            }

            getDept(row.deptId).then(response => {
                this.form = response.data;
                this.deptType = response.data.type
                let parent = response.data.parentId;
                this.title = "";
                if (response.data.type == "0") {//等于部门
                    this.open = true;
                    this.title = "查看机构部门";
                } else {
                    this.openAdmin = true
                    this.title = "查看组织机构";
                }
                // if(this.$store.state.user.info.deptId==100){
                //   this.openAdmin = true
                //   this.title = "修改组织机构";
                // }else{
                //   this.open = true;
                //   this.title = "修改机构部门";
                // }
                listDeptExcludeChild(row.deptId).then(response => {
                    //console.log(response)
                    this.deptOptions = this.handleTree(response.data, "deptId");
                    if (this.deptOptions.length == 0) {
                        const noResultsOptions = { deptId: this.form.parentId, deptName: this.form.parentName, children: [] };
                        this.deptOptions.push(noResultsOptions);
                    }
                });
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/dept/export', {
                ...this.queryParams
            }, `user_${new Date().getTime()}.xlsx`)
        },
        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "企业导入";
            this.upload.open = true;
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download('system/dept/importTemplate', {
            }, `user_template_${new Date().getTime()}.xlsx`)
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
        //pdf预览
        handlePictureCardPreviewPdf(url) {
            this.pdfUrl = pdf.createLoadingTask(url)
            // 获取页码
            this.pdfUrl.promise.then(pdf => this.pageTotal = pdf.numPages).catch(error => {
            })
            this.dialogImageUrlPdf = url;
            this.dialogVisiblePdf = true;
        },
        //文件预览
        handleView(url) {
            if (url != null && url != '' && (url).indexOf('.pdf') !== -1) {
                this.handlePictureCardPreviewPdf(url);
            } else {
                this.handlePictureCardPreview(url);
            }
        },
        handlePictureCardPreview(url) {
            this.dialogImageUrl = url;
            this.dialogVisible = true;
        },
        getId() {
            function S4() {
                return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
            }
            return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
        },
        indexMethod(index) {
            return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
        },
        /** 查询部门列表 */
        getList() {
            this.loading = true;
            this.userNewId = this.$store.state.user.info.userId
            listDept(this.queryParams).then(response => {
                console.log(response.rows)
                this.deptList = this.handleTree(response.rows, "deptId");
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 转换部门数据结构 */
        normalizer(node) {
            if (node.children && !node.children.length) {
                delete node.children;
            }
            return {
                id: node.deptId,
                label: node.deptName,
                children: node.children
            };
        },
        check(data) {
        },
        // 取消按钮 部门
        cancel() {
            // if(this.$store.state.user.info.deptId==100){
            //     this.openAdmin = false
            // }else{
            //     this.open = false;
            // }
            //this.reset();
            this.open = false;
        },
        cancelAdmin() {
            // if(this.$store.state.user.info.deptId==100){
            //     this.openAdmin = false
            // }else{
            //     this.open = false;
            // }
            //this.reset();
            this.openAdmin = false;
        },
        // 表单重置
        reset() {
            this.form = {
                deptId: null,
                parentId: null,
                ancestors: null,
                deptName: null,
                type: "1",
                companyOrigin: null,
                abbreviate: null,
                registeredFund: null,
                legalPerson: null,
                companyType: "0",
                nature: null,
                area: null,
                foundTime: null,
                businessScope: null,
                industry: null,
                contacts: null,
                address: null,
                orderNum: null,
                leader: null,
                phone: null,
                email: null,
                status: "0",
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.opt = "add";
            this.disabled = false
            this.reset();
            if (row != undefined) {
                this.form.parentId = row.deptId;
            }
            this.businessId = this.getId()
            this.refreshKey++;
            if (this.$store.state.user.info.deptId == 100) {
                this.openAdmin = true
                this.title = "添加组织机构";
            } else {
                this.open = true;
                this.title = "添加机构部门";
            }

            this.fileType = 0;

            listDept().then(response => {
                this.dept = response.rows
                this.deptOptions = this.handleTree(response.rows, "deptId");
            });
        },
        // 更多操作触发
        handleCommand(command, row) {
            switch (command) {
                case "update":
                    this.handleUpdate(row);
                    break;
                case "add":
                    this.handleAdd(row);
                    break;
                case "delete":
                    this.handleDelete(row)
                    break;
                default:
                    break;
            }
        },
        /** 展开/折叠操作 */
        toggleExpandAll() {
            this.refreshTable = false;
            this.isExpandAll = !this.isExpandAll;
            this.$nextTick(() => {
                this.refreshTable = true;
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row, index) {
            this.reset();
            this.fileType = 1;
            this.opt = "edit";
            this.isShow = false;
            this.index = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index;
            this.refreshKey++;
            if (row.fileId == "" || row.fileId == null || row.fileId == undefined) {
                this.businessId = row.deptId;
            } else {
                this.businessId = row.fileId;
            }

            getDept(row.deptId).then(response => {
                this.form = response.data;
                this.deptType = response.data.type
                let parent = response.data.parentId;
                this.title = "";
                if (response.data.type == "0") {//等于部门
                    this.open = true;
                    this.title = "修改机构部门";
                } else {
                    this.openAdmin = true
                    this.title = "修改组织机构";
                }
                // if(this.$store.state.user.info.deptId==100){
                //   this.openAdmin = true
                //   this.title = "修改组织机构";
                // }else{
                //   this.open = true;
                //   this.title = "修改机构部门";
                // }
                listDeptExcludeChild(row.deptId).then(response => {
                    //console.log(response)
                    this.deptOptions = this.handleTree(response.data, "deptId");
                    if (this.deptOptions.length == 0) {
                        const noResultsOptions = { deptId: this.form.parentId, deptName: this.form.parentName, children: [] };
                        this.deptOptions.push(noResultsOptions);
                    }
                });
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {


                if (valid) {
                    if (this.form.deptId != undefined) {
                        if (this.form.fileId == "" || this.form.fileId == null || this.form.fileId == undefined) {
                            this.form.fileId = this.businessId;
                        }
                        updateDept(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            // if(this.$store.state.user.info.deptId==100){
                            //   this.openAdmin = false
                            // }else{
                            //   this.open = false;
                            // }
                            if (this.form.type == "0") {//等于部门
                                this.open = false;
                            } else {
                                this.openAdmin = false
                            }
                            this.getList();
                            this.refreshKey++;
                        });
                    } else {
                        if (this.$store.state.user.info.userId == 1 || this.$store.state.user.info.userId == 2) {
                            this.form.parentId = 0;
                            this.form.ancestors = 0;
                            this.form.type = 1//机构
                        } else {
                            this.form.companyType = ""
                            this.form.type = 0//部门
                        }
                        this.form.fileId = this.businessId
                        addDept(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            // if(this.$store.state.user.info.deptId==100){
                            //   this.openAdmin = false
                            // }else{
                            //   this.open = false;
                            // }
                            if (this.form.type == "0") {//等于部门
                                this.open = false;
                            } else {
                                this.openAdmin = false
                            }
                            this.getList();
                            this.refreshKey++;
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$modal.confirm('是否确认删除名称为"' + row.deptName + '"的数据项？').then(function () {
                return delDept(row.deptId);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        }
    }
};
</script>
